<template>
  <el-card style="width: 500px;">
    <el-form label-width="120px" size="small" :model="form" :rules="rules" ref="pass">

      <el-form-item label="原密码" prop="password">
        <el-input v-model="state.form.password" autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="state.form.newPassword" autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmPassword">
        <el-input v-model="state.form.confirmPassword" autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import request from "../request.js";
import {ElMessage, ElNotification} from "element-plus";
import {getCurrentInstance, reactive, ref} from "vue";
import router from "../router/index.js";
const {proxy} = getCurrentInstance()

const state = reactive({
  user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
  form:{
    id:0
  },
  rules:{
    password: [
      { required: true, message: '请输入原密码', trigger: 'blur' },
      { min: 3, message: '长度不少于3位', trigger: 'blur' }
    ],
    newPassword: [
      { required: true, message: '请输入新密码', trigger: 'blur' },
      { min: 3, message: '长度不少于3位', trigger: 'blur' }
    ],
    confirmPassword: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 3, message: '长度不少于3位', trigger: 'blur' }
    ],
  }
})
const username = ref(state.user.username)



const save = async () => {

        //console.log(username.value, newPassword.value)
        await request.get("/user/UorP", {
          params:{
            username: username.value,
            password: state.form.password
          }
        }).then(res => {
          //console.log(res.data.id, res.data.password)
          if(res.data == null){
            ElMessage.error("原密码错误")
          }else{
            state.form.id = res.data.id,
            console.log(state.form.id, state.form.password)
          }
        })
        if(state.form.newPassword !== state.form.confirmPassword){
          ElMessage.error("两次密码输入不一致")
          return false
        }
        //console.log(id.value, password.value)
        await request.post("/user/password", state.form).then(res => {
          console.log(res)
            if(res.code === '200'){
              ElMessage.success("修改成功")
              router.push("/main")
            }else {
              ElMessage.error(res.msg)
            }
        })
}
</script>

<style scoped>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>